/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dll;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.Normalizer;
import java.util.Scanner;
import java.util.regex.Pattern;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Sevket
 */
public class Func_dll {

    public Connection CAD() {

        //Connection BDD

        String url = "jdbc:mysql://10.67.126.93:3306/";
        String dbName = "dico";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "";
        Connection conn = null;
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url + dbName, userName, password);


        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;


    }

    public boolean Test_File(String content) throws FileNotFoundException, SQLException {

        Connection conn = CAD();

        //On scanne le fichier.
        Scanner scanner = new Scanner(content);

        String mot = "";
        int c1 = 0;
        int c2 = 0;
        String query1 = "SELECT COUNT(mot) as compte FROM  mots WHERE ";
        StringBuilder sb = new StringBuilder();
        sb.append(query1);
        while (scanner.hasNext()) {


            c2++;
            mot = scanner.next();
            mot = mot.toLowerCase();
            mot = mot.trim();

            if (mot.length() == 1) {
                break;
            }



            // Gestion des apostrophes.
            String[] splitted = mot.split("'");
            mot = splitted[splitted.length - 1];

            // Gestion des accents
            mot = removeAccent(mot);

            //Gestion des adresses mails, si pas adresse mail, on enleve le point.
            if (!Pattern.matches("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)+$", mot)) {
                mot = mot.replaceAll("\\p{P}", ""); //On enleve les points si ce n'est pas une adresse mail.

            } else {
                c2--;
                
            }



            sb.append(" mot = '");
            sb.append(mot);
            sb.append("' OR");


            //System.out.print(mot);
            //System.out.print("\n");



        }
        sb.delete(sb.length()-3,sb.length());
        sb.append(";");
        //System.out.print(sb.toString());
        Statement st = conn.createStatement();
        
        ResultSet res = st.executeQuery(sb.toString());
        res.next();
        int nbMot = res.getInt(1);
        
        
        //Calcul du pourcentage.
        double pourcentage = (double) nbMot / (double) c2 * 100;

        System.out.print(nbMot
                + " mots sur " + c2 + " correspondent à la langue française pour un pourcentage total de " + pourcentage + " % \n");

        if (pourcentage > 50) {
            return true;
        } else {
            return false;
        }
    }

    public String removeAccent(String source) {
        return Normalizer.normalize(source, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", "");
    }

    public String getTerroristMail(String content) throws FileNotFoundException {


        Scanner scanner = new Scanner(content);
        String mail = null;
        String mot = null;
        while (scanner.hasNext()) {
            mot = scanner.next();
            mot = removeAccent(mot);
            if (Pattern.matches("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)+$", mot)) {
                mail = mot;
            }
        }

        System.out.print("Le mail du terroriste est le suivant : " + mail + "\n");
        return mail;
    }
}
